package com.wx.learn;

import java.util.Objects;

/**
 * 学生类
 */
public class collectionDemo2Student {
    private String name;
    private int age;

    public collectionDemo2Student() {
    }

    public collectionDemo2Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "\n" + "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

//    @Override
//    public boolean equals(Object o) {
//        if (this == o) return true;
//        if (o == null || getClass() != o.getClass()) return false;
//        collectionDemo2Student that = (collectionDemo2Student) o;
//        return age == that.age &&
//                Objects.equals(name, that.name);
//    }


    @Override
    public boolean equals(Object o) {
        // 1.判断是不是一个对象地址
        if(this == o) {
            return true;
        }
        // 2.判断是否为空
        if(o == null) {
            return false;
        }

        // 3.判断是否collectionDemo2Student类型
        if(o instanceof collectionDemo2Student){
            collectionDemo2Student s= (collectionDemo2Student)o;
            // 4.比较属性
            if(this.name.equals(s.getName())&&Objects.equals(this.age,s.getAge())) {
                return true;
            }
        }
        // 5.不满足
        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
